package com.baizw.cultivation.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * api配置
 *
 * @author baizw
 * @date
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    
    /** 引入Knife4j提供的扩展类 */
    private final OpenApiExtensionResolver openApiExtensionResolver;
    
    @Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }
    
    private ApiInfo apiInfo() {
        Contact contact = new Contact("剧终1204", "", "");
        return new ApiInfoBuilder()
                       .title("云舒管理后台API文档")
                       .description("更多信息请关注：https://gitee.com/bzw1204/cultivation")
                       .termsOfServiceUrl("https://gitee.com/bzw1204/cultivation")
                       .contact(contact)
                       .version("1.0")
                       .build();
    }

    @Bean
    public Docket createRestApi() {
        String groupName = "默认分组";
        return new Docket(DocumentationType.SWAGGER_2)
                       .apiInfo(apiInfo())
                       //分组名称
                       .groupName(groupName)
                       .select()
                       .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                       .paths(PathSelectors.any()).build()
                       .securitySchemes(securitySchemes())
                       .securityContexts(securityContexts())
                       //赋予插件体系
                       .extensions(openApiExtensionResolver.buildExtensions(groupName));
    }



    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                                            .securityReferences(defaultAuth())
                                            // 排除路径中包含 auth 的接口不添加认证
                                            .forPaths(PathSelectors.regex("^((?!auth).)*$"))
                                            .build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
    
}
